package top.younote.learn.sort;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @Description: 用于生成排序的数据
 * @Author: duanyu
 * @CreateTime: 2020-08-22 20:09
 **/
public class GenerateSortData {
    /**
     * 不重复随机数的文件路径
     */
    private static final String DISTINCT_FILEPATH = "./data/distinctSortNums.txt";
    /**
     * 可重复文件路径
     */
    private static final String REPEATABLE_FILEPATH = "./data/repeatableSortNums.txt";
    /**
     * 随机数的个数
     */
    private static final Integer SORT_NUMS = 1 << 10;
    /**
     * 随机数生成范围,主要要大于随机数的个数不然，可能导致死循环
     */
    private static final Integer SORT_RANGE = 1 << 14;

    public static void main(String[] args) throws IOException {
        generateDistinctSortNums();
        generateRepeatableSortNums();
    }

    /**
     * 生成不重复随机数
     *
     * @throws IOException io 异常
     */
    private static void generateDistinctSortNums() throws IOException {
        Random random = new Random();
        File distinctFile = new File(DISTINCT_FILEPATH);
        Set<Integer> distinctSet = new HashSet<>();
        while (distinctSet.size() <= SORT_NUMS) {
            int curRandomNums = random.nextInt(SORT_RANGE);
            distinctSet.add(curRandomNums);
        }
        FileUtils.writeLines(distinctFile, distinctSet, false);
    }


    /**
     * 可重复随机数文件生成
     *
     * @throws IOException
     */
    private static void generateRepeatableSortNums() throws IOException {
        Random random = new Random();
        File distinctFile = new File(REPEATABLE_FILEPATH);
        List<Integer> repeatableList = new ArrayList<>();
        while (repeatableList.size() <= SORT_NUMS) {
            int curRandomNums = random.nextInt(SORT_RANGE);
            repeatableList.add(curRandomNums);
        }
        FileUtils.writeLines(distinctFile, repeatableList, false);
    }
}
